﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Threading;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PayManage
{
    public partial class frmAttendance : Form
    {
        private static DataTable AttendanceTable;
        private static IList<AttendanceConfigEntity> AttendanceList;
        public frmAttendance()
        {
            InitializeComponent();
            AttendanceList = new List<AttendanceConfigEntity>();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
            Action openHander = new Action(OpenExcel);
            dispatcher.Invoke(openHander);
        }

        private void OpenExcel()
        {
            try
            {
                this.openFileDialog1.Filter = "Excel2003表格(*.xls)|*.xls|Excel2007表格(*.xlsx)|*.xlsx";
                this.openFileDialog1.Title = "选择表格";
                this.openFileDialog1.FileName = "";
                if (this.openFileDialog1.ShowDialog().Equals(DialogResult.OK))
                {
                    string file = this.openFileDialog1.FileName;
                    //this.dataGridView1.DataSource = DataService.GetDataTableBySheetName(file, "");
                    AttendanceTable = DataService.GetTableBySheetName(file, "");
                    AttendanceList.Clear();
                    for (int i = 0; i < AttendanceTable.Rows.Count; i++)
                    {
                        DataRow dr = AttendanceTable.Rows[i];
                        if (cvt.ToInt(dr[3]) == 1 && cvt.ToInt(dr[4]) == 2 && cvt.ToInt(dr[5]) == 3)
                        {
                            AttendanceConfigEntity attend = new AttendanceConfigEntity();
                            attend.StartRowIndex = i;
                            attend.DeptName = cvt.ToNotNull(AttendanceTable.Rows[i-1][1]);
                            AttendanceList.Add(attend);
                            if (AttendanceList.Count > 1)
                            {
                                AttendanceList[AttendanceList.Count - 2].EndRowIndex = i - 1;
                            }
                        }
                    }
                    if (AttendanceList.Count > 1)
                    {
                        AttendanceList[AttendanceList.Count - 1].EndRowIndex = AttendanceTable.Rows.Count - 1;
                    }
                }
            }
            catch (Exception ex)
            {
                DataService.ShowErrorMsg(ex.Message);
            }

        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            try
            {
                string deptName = this.txt_Dept.Text;
                string userName = this.txt_UserName.Text;
                if (string.IsNullOrEmpty(deptName))
                {
                    MessageBox.Show("输入部门名字");
                    return;

                }
                if (string.IsNullOrEmpty(userName))
                {
                    MessageBox.Show("输入员工名字");
                    return;

                }
                // Func<AttendanceConfigEntity, bool> predicate=
                //IList<AttendanceConfigEntity> attends = (IList<AttendanceConfigEntity>)AttendanceList.Where(a => deptName.IndexOf(a.DeptName) != -1);
                int Index = -1;
                foreach (var attend in AttendanceList)
                {
                    if (Index != -1)
                        break;
                    if (deptName.IndexOf(attend.DeptName) != -1)
                    {
                        for (int i = attend.StartRowIndex + 2; i < attend.EndRowIndex; i += 3)
                        {
                            if (cvt.ToNotNull(AttendanceTable.Rows[i][0]).Trim().Replace(" ", "") == userName)
                            {
                                Index = i;
                                break;
                            }
                        }
                      
                    }
                   
                }               
                if (Index != -1)
                {
                    //实际出勤
                    double realWorkDay = 0;
                    string memo = "";
                    //列查询
                    for (int i = 3; i < 34; i++)
                    {
                        //上午出勤上午是3.5 下午是4.5  3.5/8小时=0.44，  4.5/8小时=0.56
                        if (cvt.ToNotNull(AttendanceTable.Rows[Index][i]).Trim() == "√")
                        {
                            realWorkDay += 0.44;
                        }
                        if (cvt.ToNotNull(AttendanceTable.Rows[Index + 1][i]).Trim() == "√")
                        {
                            realWorkDay += 0.56;
                        }
                    }
                    memo += cvt.ToNotNull(AttendanceTable.Rows[Index + 1][34]).Trim() + ",";
                    memo += cvt.ToNotNull(AttendanceTable.Rows[Index + 2][34]).Trim() + ",";
                    memo += cvt.ToNotNull(AttendanceTable.Rows[Index + 3][34]).Trim() + ",";

                    double otherAdd = 0;
                    double oneHourse = 0;
                    double oneFiveHourse = 0;
                    string[] overTime = new string[] { cvt.ToNotNull(AttendanceTable.Rows[Index + 1][35]).Trim(),
                    cvt.ToNotNull(AttendanceTable.Rows[Index + 2][35]).Trim(),
                     cvt.ToNotNull(AttendanceTable.Rows[Index + 3][35]).Trim()
                    };
                    foreach (var s in overTime)
                    {
                        if (s.IndexOf("*4") != -1)
                        {
                           if(!s.StartsWith("*"))
                            otherAdd =cvt.ToDouble(s.Substring(0, s.Length - s.IndexOf("*")));
                        }
                        if (s.IndexOf("*8") != -1)
                        {
                            if (!s.StartsWith("*"))
                                oneHourse = cvt.ToDouble(s.Substring(0, s.Length - s.IndexOf("*")));
                        }
                        if (s.IndexOf("*10") != -1)
                        {
                            if (!s.StartsWith("*"))
                                oneFiveHourse = cvt.ToDouble(s.Substring(0, s.Length - s.IndexOf("*")));
                        }
                    }

                    this.txt_Result.Text = string.Format("实际出勤：{0}\r\n,1.0加班小时：{1}\r\n,1.5加班小时：{2}\r\n,夜班天数：{3}\r\n,备注：{4}\r\n"
                        ,realWorkDay,oneHourse,oneFiveHourse,otherAdd,memo);
                }
                else
                {
                    this.txt_Result.Text="没有查询到！";
                }

            }
            catch (Exception ex)
            {
                DataService.ShowErrorMsg(ex.Message);
            }
        }
    }
    /// <summary>
    /// 考勤DataTable中部门索引
    /// </summary>
    [Serializable]
    public class AttendanceConfigEntity
    {
        /// <summary>
        /// 考勤表中部门名称
        /// </summary>
        public string DeptName { get; set; }

        /// <summary>
        /// 考勤表中表格数据开始行
        /// </summary>
        public int StartRowIndex { get; set; }
        /// <summary>
        /// 考勤表中表格数据结束行
        /// </summary>
        public int EndRowIndex { get; set; }

    }
}
